#include "stm32f10x.h"                 // Device header
#include "Delay.h"
#include "Serial.h"
#include "DAC.h"
#include "ADC.h"
#include "PWM.h"
#include "Btn.h"

void ProcessADCData(uint16_t *buffer, uint16_t size) {
	//    // 处理ADC数据
//		Serial_SendString("ADC Value: ");
//    for (uint16_t i = 0; i < size; i++) {
//        // 例如：打印数据
//        Serial_Printf("%d ", buffer[i]);
//    }
	//首先找到最大值和最小值
	int min=buffer[0],max=buffer[0],i;
	for(i=0;i<size;i++) {
		if(min > buffer[i]) {
			min = buffer[i];
		}
		if(max < buffer[i]) {
			max = buffer[i];
		}
	}
	int vpp = max-min;
	Serial_Printf("min,max,vpp=(%d,%d,%d)\r\n",min,max,vpp);
	float five_percent = vpp*0.1;
	float top_line = max-five_percent;
	float skip_line = max-2*five_percent;
	//然后计算每两次最大值之间间隔多少个i，最多保存6个
	#define GAP_MAX_SAMPLE 10
	int top_index[GAP_MAX_SAMPLE];
	int j=0;
	for(i=0;i<size;i++) {
		if(buffer[i] > top_line) {
			if(j < GAP_MAX_SAMPLE) {
				Serial_Printf("(%d,%d) ",i, buffer[i]);
				top_index[j] = i;
				j++;
			} else {
				break;
			}
			do{
				i++;
				//Serial_Printf("[%d]",buffer[i]);
			} while(buffer[i] > skip_line);
		}
	}
	Serial_Printf("\r\nGaps:");
	int avarage = 0;
	int gap;
	for(i=1;i<GAP_MAX_SAMPLE-1;i++){
		gap = top_index[i+1]-top_index[i];
		if(gap < 5) {
			Serial_Printf("%d(discard) ",gap);
			j--;
		} else {
			avarage += gap;
			Serial_Printf("%d ",gap);
		}
	}
	if(j==2)
		return;
	avarage /= j-2;
	Serial_Printf("\r\navarage gap: %d %d\r\n",j,avarage);
	float freq = 10000/avarage;
	Serial_Printf("Freq: %f\r\n",freq);
	Serial_SendString("\r\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\r\n\r\n\r\n");
}



int PwmMode = 0;
int Period[] = {100-1,10-1,72-1};
int Prescaler[] = {7200-1,7200-1,1-1};
int InitCompare[] = {5,5,36};
int CompareMode = 1;
int SpecialCompre[] = {1,5,7};

int main(void) {
	PWM_TIM3_Init();
	//while(1);
	TIM_Cmd(TIM3, DISABLE);
	TIM3->ARR = Period[PwmMode];
	TIM3->PSC = Prescaler[PwmMode];
	TIM_SetCompare1(TIM3, InitCompare[PwmMode]);
	TIM_Cmd(TIM3, ENABLE);
	Delay_ms(1000);
	while(1) {
		if(Btn_GetKey(GPIO_Pin_7)) {
			PwmMode += 1;
			PwmMode %= 3;
			TIM_Cmd(TIM3, DISABLE);
			TIM3->ARR = Period[PwmMode];
			TIM3->PSC = Prescaler[PwmMode];
			TIM_SetCompare1(TIM3, InitCompare[PwmMode]);
			TIM_Cmd(TIM3, ENABLE);
		}
		if(Btn_GetKey(GPIO_Pin_8)) {
			if(PwmMode == 1) {
				CompareMode += 1;
				CompareMode %= 3;
				TIM_SetCompare1(TIM3, SpecialCompre[CompareMode]);
			}
		}
	}
	while(1);
	
}
int main1(void) {
	DAC_TriangleWave_Config();
	Serial_Init();
	Serial_SendString("haha");
	//
	Sampling_Init();
	//while(1);
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);
	while (1) {
			if (dma_transfer_complete) {
					dma_transfer_complete = 0; // 清除标志位
					ProcessADCData(adc_buffer, ADC_BUFFER_SIZE);
					Sampling_Restart();
			}
	}
}



//int main1(void)
//{
//	Init1();
//	GPIO_ResetBits(GPIOC,GPIO_Pin_13);  //低电平
//	GPIO_SetBits(GPIOA,GPIO_Pin_0);     //高电平
//	OLED_ShowString(1,1,"S Count: ");
//	OLED_ShowNum(1,9,CountSensor_GetCount(),3);
//	OLED_ShowString(2,1,"E Count: ");
//	OLED_ShowSignedNum(2,9,Encoder_GetCount(),3);
//	OLED_ShowString(3,1,"RunTime: ");
//	OLED_ShowNum(3,9,Run_Time_Second,6);
//	OLED_ShowString(4,1,"ExI: ");
//	OLED_ShowNum(4,5,Ext_Int_Count,4);
//	OLED_ShowNum(4,11,TIM_GetCounter(TIM3),5);
//	BitAction Led = Bit_SET;
//	while(1){
//		if(Btn_GetKey() == 1) {
//			if(Led == Bit_RESET) {
//				Led = Bit_SET;
//			} else {
//				Led = Bit_RESET;
//			}
//		}
//		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Led);
//		OLED_ShowNum(1,9,CountSensor_GetCount(),3);
//		OLED_ShowSignedNum(2,9,Encoder_GetCount(),3);
//		OLED_ShowNum(3,9,Run_Time_Second,6);
//		OLED_ShowNum(4,5,Ext_Int_Count,4);
//		OLED_ShowNum(4,11,TIM_GetCounter(TIM3),5);
//		Delay_ms(1);
//	}
//}
